#-*- coding: utf-8 -*-
#
# Copyright (C) 2007 Ville Leskinen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

"""This module provides Listener tools."""

class ListenerEvent(object):
	"""Listener event."""
	def __init__(self, length, message=None):
		"""Constructor"""
		self.length = length
		self.message = message

class Listener(object):
	"""A basic listener. """
	def __init__(self):
		self.totalLength = 0
		self.done = 0

	def addEvent(self, event):
		"""Adds event.
		
		@param event: the events that taking place
		@type event: L{igCreator.listener.ListenerEvent}"""
		self.totalLength += event.length

	def eventHappening(self, event):
		"""This should be called when event is taking place.

		@param event: the event that is taking place
		@type event: L{igCreator.listener.ListenerEvent}"""
		self.done += event.length

	def finished(self):
		"""This should be called when everything is finished."""
		self.done = self.totalLength

class Listenable(object):
	"""Base class for listenable objects."""

	def __init__(self):
		"""Constructor."""
		self.listeners = []

	def addListener(self, listener):
		"""Adds a listener for this class.

		@param listener: new listener
		@type listener: L{igCreator.listener.Listener}"""
		self.listeners.append(listener)

	def _eventHappening(self, event):
		"""Calls eventHappening function for each listener.
		
		@param event: events that currently taking place
		@type event: L{igCreator.listener.ListenerEvent}"""
		for listener in self.listeners:
			listener.eventHappening(event)
